export function genMonthCalendar(year, month) {
    const date = new Date(year, month - 1, 1, 8, 0, 0)
    console.log(date)
    console.log(date.getDate())
    // 月第一天周几
    const dayOfWeekBeginMonth = date.getDay();
    console.log('月第一天是周' + dayOfWeekBeginMonth)
    const daysInWeek = 7;

    const lastDate = new Date(year, month, 0, 8, 0, 0, 0)
    console.log(lastDate)
    // 月最后一天周几
    const dayOfWeekEndMonth = lastDate.getDay();
    console.log('月最后一天是周' + dayOfWeekBeginMonth)


    const monthBeginDate = date.getDate()
    const monthEndDate = lastDate.getDate()
    console.log('月开始是' + monthBeginDate + '号, 月结束是' + monthEndDate + '号')


    let calendars = [];

    // 填充上个月剩余的日期
    if (dayOfWeekBeginMonth > 0) {
        let preMonthDate = new Date(year, month - 1, 0, 8, 0, 0)
        let preCalendars = []
        for (let i = 0; i < dayOfWeekBeginMonth; i++) {
            const tmpDate = new Date(preMonthDate)
            preCalendars.push({
                'date': formatDate(tmpDate),
                'dayOfMonth': tmpDate.getDate(),
                'currentMonth': false
            });

            preMonthDate.setDate(tmpDate.getDate() - 1);
        }

        for(let i=dayOfWeekBeginMonth; i>0; i--){
            calendars.push(preCalendars[i - 1]);
        }
    }

    while (date <= lastDate) {
        const tmpDate = new Date(date)
        calendars.push({
            'date': formatDate(tmpDate),
            'dayOfMonth': tmpDate.getDate(),
            'currentMonth': true
        });

        date.setDate(date.getDate() + 1);
    }

    // 填充下个月剩余的日期
    if (dayOfWeekEndMonth < 6) {
        const endCount = daysInWeek - dayOfWeekEndMonth
        let nextMonthDate = lastDate
        nextMonthDate.setDate(new Date(nextMonthDate).getDate() + 1);
        let nextCalendars = []
        for (let i = 0; i < endCount - 1; i++) {
            const tmpDate = new Date(nextMonthDate)
            nextCalendars.push({
                'date': formatDate(tmpDate),
                'dayOfMonth': tmpDate.getDate(),
                'currentMonth': false
            });

            nextMonthDate.setDate(tmpDate.getDate() + 1);
        }

        for(let i=0; i<endCount - 1; i++){
            calendars.push(nextCalendars[i]);
        }
    }
    const retArray = Array.from({ length: Math.ceil(calendars.length / daysInWeek) }, (v, i) =>
        calendars.slice(i * daysInWeek, i * daysInWeek + daysInWeek)
    )
    // console.log(retArray)
    return retArray;
}

export function formatDate(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1 // 由于月份从0开始，因此需加1
    const day = date.getDate()
    return `${pad(year, 4)}-${pad(month)}-${pad(day)}`
}

export function formatDatetime(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1 // 由于月份从0开始，因此需加1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${pad(year, 4)}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
}

function pad(timeEl, total = 2, str = '0') {
    return timeEl.toString().padStart(total, str)
}

// // 使用示例
// const year = 2024;
// const month = 9;
// const calendars = genMonthCalendar(year, month);
// console.log(calendars);